home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / jrb21zoo.zoo / misc2.c < prev    next >
C/C++ Source or Header  |  1991-09-25  |  9KB  |  348 lines

  1. #ifndef LINT
  2. /* @(#) misc2.c 2.7 88/01/24 12:47:36 */
  3. static char sccsid[]="@(#) misc2.c 2.7 88/01/24 12:47:36";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  9. */
  10. #include "options.h"
  11. /* Miscellaneous routines */
  12. #include "portable.h"
  13. #include "zoo.h"
  14. #include "zooio.h"
  15. #include "various.h"
  16. #include "errors.i"
  17. #include "zoomem.h"
  18. #include "zoofns.h"     /* only for malloc */
  19.  
  20. void makepath PARMS((char *));
  21.  
  22. /**********************/
  23. /* memerr() */
  24. /* Give error message on memory error and abort */
  25. void memerr(i)
  26. unsigned int i;
  27. {
  28. #ifdef OOZ
  29.    prterror ('f', no_memory, "", "");
  30. #else
  31.     if (i != 0)
  32.         prterror ('f', no_memory);
  33.     else
  34.         prterror('f', "needed %u bytes: %s", no_memory);
  35. #endif
  36. }
  37.  
  38. /**********************/
  39. /*
  40. emalloc() allocates memory like malloc() does (and it calls malloc).  However,
  41. it automatically calls the error function memerr() if memory couldn't be
  42. allocated.  It also assumes that memory will never be freed and conserves it
  43. by allocating memory in large chunks and then partitioning it out with no
  44. administrative overhead.  This avoids many problems due to various bugs in
  45. various implementations of malloc, and also avoids a very high amount of
  46. malloc overhead for repeated allocations to store numerous filenames etc.
  47. The down side is that we can't use free().
  48.  
  49. WARNING:  No alignment of allocated memory is attempted, so memory
  50. allocated through emalloc should be used only for storing strings.
  51. For allocating memory for other data types use ealloc().  */
  52.  
  53. VOIDPTR emalloc (size)
  54. unsigned int size;
  55. {
  56. #define  BLOCK_SIZE  512      /* memory allocation granularity */
  57.  
  58. #ifdef USE_MALLOC
  59. /* Pass on memory requests to malloc() */
  60.    char *ptr;
  61.    if ((ptr = malloc (size)) == NULL)
  62.       memerr(size);
  63.    return (ptr);
  64. #else
  65.    static char *memptr;
  66.    static unsigned avail = 0;
  67.    unsigned malloc_incr;
  68.    char *retval;
  69.  
  70.    if (size == 0)
  71.       return (NULL);
  72.  
  73.    /* if not enough space avail get some more */
  74.    if (avail < size) {
  75.       malloc_incr = BLOCK_SIZE;
  76.       if (malloc_incr < size)
  77.          malloc_incr = size;
  78.       while (malloc_incr >= size && (memptr = malloc (malloc_incr)) == NULL)
  79.          malloc_incr = (malloc_incr / 6) * 5;
  80.       avail = malloc_incr;
  81.    }
  82.  
  83.    if (avail < size)
  84.       memerr(size);                             /* no return from this */
  85.    retval = memptr;
  86.    memptr += size;
  87.    avail -= size;
  88.    return (retval);
  89. #endif  /* end of not USE_MALLOC */
  90. }
  91.  
  92. /**********************/
  93. /*
  94. ealloc() is just a wrapper around malloc(), which causes memerr() to be
  95. called if memory cannot be allocated.  All memory requests are passed on
  96. to malloc.   Allocated memory can later be freed.  */
  97.  
  98. VOIDPTR ealloc(size)
  99. unsigned int size;
  100. {
  101.    char *ptr;
  102.    if ((ptr = malloc (size)) == NULL)
  103.       memerr(size);
  104.    return ptr;
  105. }
  106.  
  107. /**********************/
  108. /* erealloc() is a wrapper around realloc() the way ealloc is a wrapper
  109. around malloc().  It calls memerr() on error. */
  110.  
  111. VOIDPTR erealloc(p, size)
  112. VOIDPTR p;
  113. unsigned int size;
  114. {
  115.    char *ptr;
  116.    if ((ptr = realloc (p, size)) == NULL)
  117.       memerr(size);
  118.    return ptr;
  119. }
  120.  
  121. /**********************/
  122. /* putstr()
  123. This function prints a string to standard output.  If the received
  124. string pointer is NULL, it is handled safely.  This function is here
  125. for historical reasons:  Ooz was once coded to not use printf under
  126. MSDOS to save space, and at that time putstr() printed a string
  127. without using printf.  It should eventually be eliminated and all
  128. calls to it replaced with calls to printf directly.
  129. */
  130. void putstr (str)
  131. register char *str;
  132. {
  133.    if (str == NULL)
  134.       return;
  135.     printf ("%s", str);
  136. }
  137.  
  138. /**********************/
  139. /* exists()
  140. This function checks the existence of a file.  
  141.  
  142. If the symbol EXISTS is defined, that is called as a macro and
  143. supplied the filename.  It must return 1 if the file exists and
  144. 0 if it does not.
  145.  
  146. If EXISTS is not defined, exists() tests to see if the file can be 
  147. opened for reading or writing;  if so, it returns 1 else it returns 0. 
  148.  
  149. Because of the delay between the time existence is checked and the time Zoo
  150. creates a files, a race condition exists.  It would be better to
  151. use open() with the O_EXCL flag but that will not work for many
  152. systems.
  153. */
  154.  
  155. int exists (fname)
  156. char *fname;
  157. {
  158. #ifdef EXISTS
  159.     return EXISTS(fname);
  160. #else
  161.    ZOOFILE f;
  162.  
  163.    if ( (f = zooopen (fname, Z_READ )) != NOFILE ||
  164.           (f = zooopen (fname, Z_WRITE)) != NOFILE ) {
  165.       zooclose (f);
  166.       return (1);
  167.    } else
  168.       return (0);
  169. #endif /* ifdef EXISTS */
  170. }
  171.  
  172. /****************
  173. newcat() allocates enough space to concatenate two strings then returns
  174. a pointer to the concatenated result */
  175.  
  176. char *newcat (r, s)
  177. char *r, *s;
  178. {
  179.    char *temp = emalloc (strlen (r) + strlen (s) + 2); /* 1 spare */
  180.    strcpy (temp, r);
  181.    strcat (temp, s);
  182.    return (temp);
  183. }
  184.  
  185.  
  186. /* Creates a path */
  187. void makepath(path)
  188. char *path;
  189. {
  190.    char tmppath[PATHSIZE];
  191.    char *slashpos;
  192.    if (path == NULL)
  193.       return;
  194. #if 0
  195.    while (*lastptr(path) == *(char *) PATH_CH)  /* remove trailing slashes */
  196.       *lastptr(path) = '\0';
  197. #else
  198.    while (((slashpos = lastptr(path)) != NULL) &&
  199.       (*slashpos == *(char *) PATH_CH))  /* remove trailing slashes */
  200.       *slashpos = '\0';
  201. #endif
  202.  
  203.    if (*path == '\0')
  204.       return;
  205.  
  206.    slashpos = findlast(path, PATH_CH);    /* find last slash */
  207.    if (slashpos == NULL) {                /* if not, just create dir. */
  208.       MKDIR(path);
  209.       return;
  210.    } else {                               /* otherwise...         */
  211.       if (slashpos == path) {             /* if leading slash */
  212.          MKDIR(slashpos);                 /* make that directory */
  213.          return;                          /* and done */
  214.       } else {
  215.          strcpy(tmppath,path);            /* save path */
  216.          *slashpos = '\0';                /* split into prefix & suffix */
  217. #ifdef DEBUG
  218.          printf("making path from [%s]\n", path);
  219. #endif
  220.          makepath(path);                     /* make path from prefix */
  221. #ifdef DEBUG
  222.          printf("making dir from [%s]\n", tmppath);
  223. #endif
  224.          MKDIR(tmppath);                  /* make dir from suffix */
  225.       }
  226.    }
  227. } /* makepath() */
  228.  
  229. /*
  230. If no extension in filename add supplied extension
  231. */
  232. char *addext (fname, ext)
  233. char *fname;
  234. char *ext;
  235. {
  236.    if (strchr (nameptr (fname), EXT_CH) == NULL)
  237.       return (newcat (fname, ext));
  238.    else
  239.       return (fname);
  240. }
  241.  
  242. #ifdef VER_CH       /* remove any trailing extension field */
  243. char *strip_ver (fname)
  244. char *fname;
  245. {
  246.    char *p = strchr (fname, VER_CH);
  247.    if (p != NULL)
  248.       *p = '\0';
  249. }
  250. #endif
  251.  
  252. /*
  253. Function samefile() compares two filenames to see if they are the
  254. same file.  Just strcmp() or str_icmp() could have been used, except
  255. that if the filenames have trailing version fields, we want to
  256. compare those always equal.  samefile() is called by routines
  257. that want to avoid adding an archive to itself.
  258. */
  259. int samefile (f1, f2)
  260. char *f1;
  261. char *f2;
  262. {
  263. #ifdef IGNORECASE
  264. #define COMPARE str_icmp
  265. #else
  266. #define COMPARE strcmp
  267. #endif
  268.  
  269. #ifdef VER_CH
  270.    char tf1[LFNAMESIZE];
  271.    char tf2[LFNAMESIZE];
  272.    strcpy (tf1, f1);
  273.    strcpy (tf2, f2);
  274.    strip_ver (tf1);   /* strip version fields */
  275.    strip_ver (tf2);
  276.    return (COMPARE (tf1, tf2) == 0);
  277. #else
  278. /* if no version fields, just use strcmp(i) */
  279.    return (COMPARE (f1, f2) == 0);
  280. #endif
  281. }
  282.  
  283. #ifdef USE_ASCII
  284. int isdigit (c)
  285. int c;
  286. {
  287.     return (c >= '0' && c <= '9');
  288. }
  289. int isupper (c)
  290. int c;
  291. {
  292.     return (c >= 'A' && c <= 'Z');
  293. }
  294.  
  295. int toascii (c)
  296. int c;
  297. {
  298.     return (c & 0x7f);
  299. }
  300.  
  301. int tolower (c)
  302. int c;
  303. {
  304.     return (isupper(c) ? (c | 0x20) : c);
  305. }
  306. #endif
  307.  
  308. #ifdef GETTZ
  309. /****************
  310. Function tzadj() accepts a directory entry and adjusts its timestamp
  311. to reflect its timezone.  Uses function mstime() from mstime.i
  312. and mstonix() from nixtime.i.
  313. */
  314.  
  315. long mstonix PARMS((unsigned int, unsigned int));
  316. long gettz PARMS((void));
  317. #include "mstime.i"    /* get mstime() */
  318.  
  319. void tzadj (direntry)
  320. struct direntry *direntry;
  321. {
  322.     long diff_tz;
  323.     long longtime;
  324.     if (direntry->tz == NO_TZ)        /* none stored */
  325.         return;
  326.     diff_tz = (long) direntry->tz * (3600/4) - gettz(); /* diff. in seconds */
  327.     longtime = mstonix (direntry->date, dir